﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="QueryExecutor.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.Persistor.Internal
{
  using System.Collections.Generic;
  using System.Net;

  using DataDuctus.Snmp.Persistor.Core.Extensions;
  using DataDuctus.Snmp.Persistor.Core.MetaData;

  using Lextm.SharpSnmpLib;
  using Lextm.SharpSnmpLib.Messaging;

  /// <summary>
  /// The query executor.
  /// </summary>
  public class QueryExecutor
  {
    #region Fields

    /// <summary>
    /// The clz meta.
    /// </summary>
    private readonly ClassMeta clzMeta;

    #endregion

    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="QueryExecutor"/> class.
    /// </summary>
    /// <param name="clzMeta">
    /// The clz meta.
    /// </param>
    public QueryExecutor(ClassMeta clzMeta)
    {
      this.clzMeta = clzMeta;
    }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    /// The execute.
    /// </summary>
    /// <param name="connectionMeta">
    /// The connection meta.
    /// </param>
    /// <returns>
    /// The System.Collections.Generic.IList`1[T -&gt; Lextm.SharpSnmpLib.Variable].
    /// </returns>
    public IList<Variable> Execute(ConnectionMeta connectionMeta)
    {
      var target = new IPEndPoint(connectionMeta.Host.ToIpAddress(), connectionMeta.Port);

      IList<Variable> result = new List<Variable>();

      var baseOid = new ObjectIdentifier(this.clzMeta.Oid);
      var securityOid = new OctetString(connectionMeta.SecurityString);

      Messenger.Walk(
        connectionMeta.Version, target, securityOid, baseOid, result, connectionMeta.Timeout, WalkMode.WithinSubtree);

      return result;
    }

    #endregion
  }
}